﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Plant
namespace backoffice.Data
{
    partial class Plant
    {
        public void setPlant(Plant plant)
        {
            this.BotanicalFamily = plant.BotanicalFamily;
            this.Buys = plant.Buys;
            this.Description = plant.Description;
            this.Environment = plant.Environment;
            this.Experts = plant.Experts;
            this.Fertilizer = plant.Fertilizer;
            this.FlowerColor = plant.FlowerColor;
            this.Flowering = plant.Flowering;
            this.Foliage = plant.Foliage;
            this.Fragrant = plant.Fragrant;
            this.GrowingEase = plant.GrowingEase;
            this.GrowthRate = plant.GrowthRate;
            this.Height = plant.Height;
            this.Humidity = plant.Humidity;
            this.Image = plant.Image;
            this.IsGenus = plant.IsGenus;
            this.LightingNeeds = plant.LightingNeeds;
            this.Name = plant.Name;
            this.Nutrient = plant.Nutrient;
            this.OtherNames = plant.OtherNames;
            this.Pests = plant.Pests;
            this.PlantID = plant.PlantID;
            this.Propagation = plant.Propagation;
            this.Protocols = plant.Protocols;
            this.Questions = plant.Questions;
            this.ScientificName = plant.ScientificName;
            this.Season = plant.Season;
            this.Soil = plant.Soil;
            this.SoilType = plant.SoilType;
            this.Temperature = plant.Temperature;
            this.Toxicity = plant.Toxicity;
            this.Type = plant.Type;
            this.Uses = plant.Uses;
            this.Watering = plant.Watering;
            this.Width = plant.Width;
            this.State = plant.State;

        }
    }
}
#endregion

#region dataPlant
namespace backoffice.Data
{
    public class dataPlant
    {
        #region Create
        public static void createPlant(Plant p)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                p.PlantID = Guid.NewGuid();

                db.Plants.InsertOnSubmit(p);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Plant getPlant(Guid guid)
        {
            try
            {
                if (guid == null) return null;

                db2growDataContext db = new db2growDataContext();

                Plant plant = db.Plants.FirstOrDefault(p => p.PlantID == guid);

                return plant;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        
        public static Plant getPlant(Expression<Func<Plant, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Plants.Where(whereClause).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Plant> getAllPlants()
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Plants;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Plant> getPlants(Expression<Func<Plant, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Plants.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static KeyValuePair<Soil, Nutrient> getPlantInfo(Plant p)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                Soil s = dataSoil.getSoil(p.PlantID);
                Nutrient n = dataNutrient.getNutrient(p.PlantID);

                KeyValuePair<Soil, Nutrient> pair = new KeyValuePair<Soil, Nutrient>(s, n);

                return pair;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IDictionary<Plant, KeyValuePair<Soil, Nutrient>> getPlantAndInfo(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                Soil s = dataSoil.getSoil(guid);
                Nutrient n = dataNutrient.getNutrient(guid);
                KeyValuePair<Soil, Nutrient> soil_nutrient = new KeyValuePair<Soil, Nutrient>(s, n);

                IDictionary<Plant, KeyValuePair<Soil, Nutrient>> plant_info = new Dictionary<Plant, KeyValuePair<Soil, Nutrient>>();
                plant_info.Add(getPlant(guid), soil_nutrient);

                return plant_info;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Plants_Disease> getPlant_Disease(Expression<Func<Plants_Disease, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Plants_Diseases.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Plants_Method> getPlant_Method(Expression<Func<Plants_Method, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Plants_Methods.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Plants_Pathology> getPlant_Pathology(Expression<Func<Plants_Pathology, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Plants_Pathologies.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changePlant(Plant plant)
        {
            try
            {
                if (plant == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Plant res = db.Plants.FirstOrDefault( p => p.PlantID == plant.PlantID);

                    if(res == null) return false;
                    else
                    {
                        res.setPlant(plant);
                        db.SubmitChanges();
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deletePlant(Plant p)
        {
            {
                try
                {
                    db2growDataContext db = new db2growDataContext();

                    if (db.Plants.Contains(p))
                    {
                        Plant res = db.Plants.FirstOrDefault(plant => plant.PlantID == p.PlantID);

                        if (res == null) return false;
                        else
                        {
                            // Update plant
                            p.State = false;
                            db.SubmitChanges();
                            return true;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
        #endregion
    }
}
#endregion
